import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:lh_shop_flutter/config/app_style.dart';
import 'package:lh_shop_flutter/page/cart/shopping_cart_page.dart';
import 'package:lh_shop_flutter/page/group/group_booking_page.dart';
import 'package:lh_shop_flutter/page/home/home_page.dart';
import 'package:lh_shop_flutter/page/mine/me_page.dart';
import 'package:lh_shop_flutter/provider/tabbar_index_provider.dart';
import 'package:lh_shop_flutter/r.g.dart';
import 'package:provider/provider.dart';

///
///Author: YYF
///Date: 2020-12-19 12:34:03
///Description: Tabbar
///

class TabPage extends StatefulWidget {
  @override
  _TabPageState createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  final _tabBodies = [
    HomePage(),
    GroupBookingPage(),
    ShoppingCartPage(),
    MePage()
  ];

  final _tabbarTitles = ["首页", "拼团", "购物车", "我的"];
  final _tabbarDefaultImgs = [
    R.image.tab_home_icon().assetName,
    R.image.tab_gropu_icon().assetName,
    R.image.tab_cart_icon().assetName,
    R.image.tab_mine_icon().assetName,
  ];
  final _tabbarSelectedImgs = [
    R.image.tab_home_icon_selected().assetName,
    R.image.tab_gropu_icon_selected().assetName,
    R.image.tab_cart_icon_selected().assetName,
    R.image.tab_mine_icon_selected().assetName,
  ];
  @override
  Widget build(BuildContext context) {
    return Consumer<TabbarSelectedIndexProvider>(
      builder: (context, value, child) {
        print(value.currentIndex);
        return Scaffold(
            backgroundColor: Colors.white,
            bottomNavigationBar: BottomNavigationBar(
              backgroundColor: Colors.white,
              type: BottomNavigationBarType.fixed,
              selectedItemColor: AppColors.mainColor,
              selectedFontSize: 12,
              items: _tabbarTitles
                  .asMap()
                  .keys
                  .map(
                    (index) => BottomNavigationBarItem(
                        icon: Image.asset(_tabbarDefaultImgs[index]),
                        activeIcon: Image.asset(_tabbarSelectedImgs[index]),
                        label: _tabbarTitles[index]),
                  )
                  .toList(),
              currentIndex: value.currentIndex,
              onTap: (index) {
                if (index == 3) {
                  SystemChrome.setSystemUIOverlayStyle(
                      SystemUiOverlayStyle.light);
                } else {
                  SystemChrome.setSystemUIOverlayStyle(
                      SystemUiOverlayStyle.dark);
                }
                Provider.of<TabbarSelectedIndexProvider>(context, listen: false)
                    .changeCurrentInde(index);
              },
            ),
            body: IndexedStack(
              children: _tabBodies,
              index: value.currentIndex,
            ));
      },
    );
  }
}
